# Transparent Corruption: The effect of illicit connections and trusted references 
# on the demand for bureaucratic intermediation

# Authors: Jose Ramon Morales-Arilla & Ana Gabriela Ibarra Luces

# SCRIPT 03 - FIGURES


# Figure 1 - Take-up rate by corruption suggestion category ---------------

# Generate labels for axis
Sug_Corr <- c("No suggestion","Suggestion")

# Set coefficients of interest for bars
Y <- c(reg_1_c$coefficients[1], (reg_1_c$coefficients[1] + reg_1_c$coefficients[2]))

graph_1 <- data.frame(Y, Sug_Corr)

# Set up confidence intervals (minimum and maximum)
y_min <- c(-2, reg_1_c$coefficients[1] + reg_1_c$coefficients[2] - reg_1_c$se[1]*1.96)
y_max <- c(-2, reg_1_c$coefficients[1] + reg_1_c$coefficients[2] + reg_1_c$se[1]*1.96)

# Plot 
fig_1 <- ggplot(graph_1, aes(x = factor(Sug_Corr), y = Y, fill = factor(Sug_Corr))) +
   scale_fill_brewer(palette = "Paired") +
   geom_bar(stat = "identity", position = "dodge") + 
   geom_errorbar(aes(ymin = y_min, ymax = y_max), width=.2,
                 position=position_dodge(.9)) +
   geom_text(aes(y = Y, label = round(Y, digits = 3), family = "serif"), colour = "black", size = 10,
             vjust = 7, position = position_dodge(.9))+
   labs(x = "\nCorruption", 
        y = "Take-up rate agency services\n") +
   theme_minimal() +
   theme(legend.position = "none", 
         text=element_text(size=35,  family="serif")) + 
   ylim(0,0.8)

ggsave('Data/Output/Figures/fig_1_transparent_corruption.pdf',width=10, height=10)


# Figure 2 - Take-up rates by price levels and corruption suggestions --------

# Generate labels for axis
Sug_Corr <- c("No suggestion","Suggestion","Suggestion","No suggestion")
Precio <- c("Low", "High", "Low", "High")

# Set coefficients of interest for bars
Y <- c(reg_5_c$coefficients[1], 
       (reg_5_c$coefficients[1] + reg_5_c$coefficients[2] + reg_5_c$coefficients[3] +reg_5_c$coefficients[4]), 
       (reg_5_c$coefficients[1] + reg_5_c$coefficients[2]), 
       (reg_5_c$coefficients[1] + reg_5_c$coefficients[3]))

graph_2a <- data.frame(Y, Sug_Corr, Precio) %>% dplyr::filter(Sug_Corr == "No suggestion")

# Set up confidence intervals (minimum and maximum)
y_mina <- c(-2, reg_5_c$coefficients[1] + reg_5_c$coefficients[3] - reg_5_c$se[1]*1.96)
y_maxa <- c(-2, reg_5_c$coefficients[1] + reg_5_c$coefficients[3] + reg_5_c$se[1]*1.96)


# Plot by grids
a <- ggplot(graph_2a, aes(x = reorder(factor(Precio), -Y), y = Y, fill = factor(Precio))) +
   scale_fill_brewer(palette = "Paired") +
   geom_bar(stat = "identity", position = "dodge") + 
   geom_errorbar(aes(ymin = y_mina, ymax = y_maxa), width=.2,
                 position=position_dodge(.9)) +
   geom_text(aes(y = Y, label = round(Y, digits = 3), family = "serif"), colour = "black", size = 6,
             vjust = 7, position = position_dodge(.9))+
   labs(x = "\nPrice", 
        y = "Take-up rate agency services\n",
        title = 'No corruption suggestion') +
   theme_minimal() +
   theme(legend.position = "none", 
         text=element_text(size=18,  family="serif"),
         plot.title = element_text(hjust = 0.5)) + 
   ylim(0,0.8)


graph_2b <- data.frame(Y, Sug_Corr, Precio) %>% dplyr::filter(Sug_Corr == "Suggestion")

# Set up confidence intervals (minimum and maximum)
y_minb <- c(reg_5_c$coefficients[1] + reg_5_c$coefficients[2] + reg_5_c$coefficients[3] +reg_5_c$coefficients[4] - reg_5_c$se[1]*1.96, -2)

y_maxb <- c(reg_5_c$coefficients[1] + reg_5_c$coefficients[2] + reg_5_c$coefficients[3] +reg_5_c$coefficients[4] + reg_5_c$se[1]*1.96, -2)


b <- ggplot(graph_2b, aes(x = reorder(factor(Precio), -Y), y = Y, fill = factor(Precio))) +
   scale_fill_brewer(palette = "Paired") +
   geom_bar(stat = "identity", position = "dodge") + 
   geom_errorbar(aes(ymin = y_minb, ymax = y_maxb), width=.2,
                 position=position_dodge(.9)) +
   geom_text(aes(y = Y, label = round(Y, digits = 3), family = "serif"), colour = "black", size = 6,
             vjust = 9, position = position_dodge(.9))+
   labs(x = "\nPrice", 
        y = " ",
        title = 'Corruption suggestion') +
   theme_minimal() +
   theme(legend.position = "none", 
         text=element_text(size=18,  family="serif"),
         plot.title = element_text(hjust = 0.5)) + 
   ylim(0,0.8)

Sug_Corr <- c("No suggestion","Suggestion")
Precio <- c("High","High")
Y <- c( reg_5_c$coefficients[3], 
        (reg_5_c$coefficients[3] + reg_5_c$coefficients[4]))


graph_2c <- data.frame(Y, Sug_Corr, Precio) %>% dplyr::filter(Precio == "High")

# Set up confidence intervals (minimum and maximum)
y_minc <- c(-2, reg_5_c$coefficients[3] + reg_5_c$coefficients[4] - reg_5_c$se[3]*1.96)

y_maxc <- c(-2, reg_5_c$coefficients[3] + reg_5_c$coefficients[4] + reg_5_c$se[3]*1.96)

c <- ggplot(graph_2c, aes(x = reorder(factor(Sug_Corr), -Y), y = Y, fill = factor(Sug_Corr))) +
   scale_fill_brewer(palette = "Paired") +
   geom_bar(stat = "identity", position = "dodge") + 
   geom_errorbar(aes(ymin = y_minc, ymax = y_maxc), width=.2,
                 position=position_dodge(.9)) +
   geom_text(aes(y = Y, label = round(Y, digits = 3), family = "serif"), colour = "black", size = 6,
             vjust = -6, position = position_dodge(.9))+
   labs(x = "\nCorruption suggestion", 
        y = " ",
        title = 'Effect of price on take-up') +
   theme_minimal() +
   theme(legend.position = "none", 
         text=element_text(size=18,  family="serif"),
         plot.title = element_text(hjust = 0.5))+ 
   ylim(-0.4, 0)

# Plot using patchwork (grids)
fig_2 <- a + b + c


ggsave('Data/Output/Figures/fig_2_transparent_corruption.pdf',width=20, height=10)

# Figure 3 - Take-up rates by reference type and corruption suggestion -------

# Generate labels for axis
Sug_Corr <- c("No suggestion","Suggestion","Suggestion","No suggestion")
GestLink <- c("Online", "Trusted", "Online","Trusted")

# Set coefficients of interest for bars
Y <- c(reg_9_c$coefficients[1], 
       (reg_9_c$coefficients[1] + reg_9_c$coefficients[2] + reg_9_c$coefficients[3] +reg_9_c$coefficients[4]), 
       (reg_9_c$coefficients[1] + reg_9_c$coefficients[2]), 
       (reg_9_c$coefficients[1] + reg_9_c$coefficients[3]))


graph_2d <- data.frame(Y, Sug_Corr, GestLink) %>% dplyr::filter(Sug_Corr == "No suggestion")

# Set up confidence intervals (minimum and maximum)
y_mind <- c(-2, reg_9_c$coefficients[1] + reg_9_c$coefficients[3] - reg_9_c$se[1]*1.96)
y_maxd <- c(-2, reg_9_c$coefficients[1] + reg_9_c$coefficients[3] + reg_9_c$se[1]*1.96)

# Plot by grids
d <- ggplot(graph_2d, aes(x = reorder(factor(GestLink), Y), y = Y, fill = factor(GestLink))) +
   scale_fill_brewer(palette = "Paired") +
   geom_bar(stat = "identity", position = "dodge") + 
   geom_errorbar(aes(ymin = y_mind, ymax = y_maxd), width=.2,
                 position=position_dodge(.9)) +
   geom_text(aes(y = Y, label = round(Y, digits = 3), family = "serif"), colour = "black", size = 6,
             vjust = 7, position = position_dodge(.9))+
   labs(x = "\nReference", 
        y = "Take-up rate agency services\n",
        title = 'No corruption suggestion') +
   theme_minimal() +
   theme(legend.position = "none", 
         text=element_text(size=16,  family="serif"),
         plot.title = element_text(hjust = 0.5)) + 
   ylim(0,0.8)



graph_2e <- data.frame(Y, Sug_Corr, GestLink) %>% dplyr::filter(Sug_Corr == "Suggestion")

# Set up confidence intervals (minimum and maximum)
y_mine <- c(reg_9_c$coefficients[1] + reg_9_c$coefficients[2] + reg_9_c$coefficients[3] +reg_9_c$coefficients[4] - reg_9_c$se[1]*1.96, -2)

y_maxe <- c(reg_9_c$coefficients[1] + reg_9_c$coefficients[2] + reg_9_c$coefficients[3] +reg_9_c$coefficients[4] + reg_9_c$se[1]*1.96, -2)


e <- ggplot(graph_2e, aes(x = reorder(factor(GestLink), -Y), y = Y, fill = factor(GestLink))) +
   scale_fill_brewer(palette = "Paired") +
   geom_bar(stat = "identity", position = "dodge") + 
   geom_errorbar(aes(ymin = y_mine, ymax = y_maxe), width=.2,
                 position=position_dodge(.9)) +
   geom_text(aes(y = Y, label = round(Y, digits = 3), family = "serif"), colour = "black", size = 6,
             vjust = 9, position = position_dodge(.9))+
   labs(x = "\nReference", 
        y = " ",
        title = 'Corruption suggestion') +
   theme_minimal() +
   theme(legend.position = "none", 
         text=element_text(size=16,  family="serif"),
         plot.title = element_text(hjust = 0.5)) + 
   ylim(0,0.8)

Sug_Corrb <- c("No suggestion","Suggestion")
GestLinkb <- c("Trusted","Trusted")
Y <- c( reg_9_c$coefficients[3], 
        (reg_9_c$coefficients[3] + reg_9_c$coefficients[4]))


graph_2f <- data.frame(Y, Sug_Corrb, GestLinkb) 

# Set up confidence intervals (minimum and maximum)
y_minf <- c(-2, reg_9_c$coefficients[3] + reg_9_c$coefficients[4] - reg_9_c$se[3]*1.96)

y_maxf <- c(-2, reg_9_c$coefficients[3] + reg_9_c$coefficients[4] + reg_9_c$se[3]*1.96)

f <- ggplot(graph_2f, aes(x = reorder(factor(Sug_Corrb), -Y), y = Y, fill = factor(Sug_Corrb))) +
   scale_fill_brewer(palette = "Paired") +
   geom_bar(stat = "identity", position = "dodge") + 
   geom_errorbar(aes(ymin = y_minf, ymax = y_maxf), width=.2,
                 position=position_dodge(.9)) +
   geom_text(aes(y = Y, label = round(Y, digits = 3), family = "serif"), colour = "black", size = 6,
             vjust = -2, position = position_dodge(.9)) +
   labs(x = "\nCorruption suggestion", 
        y = " ",
        title = 'Effect of reference on take-up') +
   theme_minimal() +
   theme(legend.position = "none", 
         text=element_text(size=14,  family="serif"),
         plot.title = element_text(hjust = 0.5))+ 
   ylim(-0.2, 0.2)

# Plot using patchwork (grids)
fig_3 <- d + e + f


ggsave('Data/Output/Figures/fig_3_transparent_corruption.pdf',width=20, height=10)

# Figure 4 -  Take-up rate by price levels reference type ---------------

# Generate labels for axis
GestLink <- c("Online","Trusted","Trusted","Online")
Price <- c("Low", "High", "Low","High")

# Set coefficients of interest for bars
Y <- c(reg_1_s$coefficients[1], 
       (reg_1_s$coefficients[1] + reg_1_s$coefficients[2] + reg_1_s$coefficients[3] +reg_1_s$coefficients[4]), 
       (reg_1_s$coefficients[1] + reg_1_s$coefficients[2]), 
       (reg_1_s$coefficients[1] + reg_1_s$coefficients[3]))


graph_2d <- data.frame(Y, Price, GestLink) %>% dplyr::filter(GestLink == "Online")

# Set up confidence intervals (minimum and maximum)
y_mind <- c(-2, reg_1_s$coefficients[1] + reg_1_s$coefficients[3] - reg_1_s$se[1]*1.96)
y_maxd <- c(-2, reg_1_s$coefficients[1] + reg_1_s$coefficients[3] + reg_1_s$se[1]*1.96)

# Plot by grids
d <- ggplot(graph_2d, aes(x = reorder(factor(Price), -Y), y = Y, fill = factor(Price))) +
   scale_fill_brewer(palette = "Paired") +
   geom_bar(stat = "identity", position = "dodge") + 
   geom_errorbar(aes(ymin = y_mind, ymax = y_maxd), width=.2,
                 position=position_dodge(.9)) +
   geom_text(aes(y = Y, label = round(Y, digits = 3), family = "serif"), colour = "black", size = 6,
             vjust = 7, position = position_dodge(.9))+
   labs(x = "\nPrice", 
        y = "Take-up rate agency services\n",
        title = 'Online reference') +
   theme_minimal() +
   theme(legend.position = "none", 
         text=element_text(size=16,  family="serif"),
         plot.title = element_text(hjust = 0.5)) + 
   ylim(0,0.8)



graph_2e <- data.frame(Y, Price, GestLink) %>% dplyr::filter(GestLink == "Trusted")

# Set up confidence intervals (minimum and maximum)
y_mine <- c(reg_1_s$coefficients[1] + reg_1_s$coefficients[2] + reg_1_s$coefficients[3] +reg_1_s$coefficients[4] - reg_1_s$se[1]*1.96, -2)
y_maxe <- c(reg_1_s$coefficients[1] + reg_1_s$coefficients[2] + reg_1_s$coefficients[3] +reg_1_s$coefficients[4] + reg_1_s$se[1]*1.96, -2)


e <- ggplot(graph_2e, aes(x = reorder(factor(Price), -Y), y = Y, fill = factor(Price))) +
   scale_fill_brewer(palette = "Paired") +
   geom_bar(stat = "identity", position = "dodge") + 
   geom_errorbar(aes(ymin = y_mine, ymax = y_maxe), width=.2,
                 position=position_dodge(.9)) +
   geom_text(aes(y = Y, label = round(Y, digits = 3), family = "serif"), colour = "black", size = 6,
             vjust = 9, position = position_dodge(.9))+
   labs(x = "\nPrice", 
        y = " ",
        title = 'Trusted reference') +
   theme_minimal() +
   theme(legend.position = "none", 
         text=element_text(size=16,  family="serif"),
         plot.title = element_text(hjust = 0.5)) + 
   ylim(0,0.8)

Priceb <- c("High","High")
GestLinkb <- c("Online","Trusted")
Y <- c( reg_1_s$coefficients[3], 
        (reg_1_s$coefficients[3] + reg_1_s$coefficients[4]))


graph_2f <- data.frame(Y, Priceb, GestLinkb) 

# Set up confidence intervals (minimum and maximum)
y_minf <- c(-2, reg_1_s$coefficients[3] + reg_1_s$coefficients[4] - reg_1_s$se[3]*1.96)
y_maxf <- c(-2, reg_1_s$coefficients[3] + reg_1_s$coefficients[4] + reg_1_s$se[3]*1.96)

f <- ggplot(graph_2f, aes(x = reorder(factor(GestLinkb), Y), y = Y, fill = factor(GestLinkb))) +
   scale_fill_brewer(palette = "Paired") +
   geom_bar(stat = "identity", position = "dodge") + 
   geom_errorbar(aes(ymin = y_minf, ymax = y_maxf), width=.2,
                 position=position_dodge(.9)) +
   geom_text(aes(y = Y, label = round(Y, digits = 3), family = "serif"), colour = "black", size = 6,
             vjust = -2, position = position_dodge(.9)) +
   labs(x = "\nReference", 
        y = " ",
        title = 'Effect on price elasticity') +
   theme_minimal() +
   theme(legend.position = "none", 
         text=element_text(size=16,  family="serif"),
         plot.title = element_text(hjust = 0.5))+ 
   ylim(-0.3, 0.1)

# Plot using patchwork (grids)
fig_4 <- d + e + f


ggsave('Data/Output/Figures/fig_4_transparent_corruption.pdf',width=20, height=10)

# Figure A.3 - Heterogeneity in the effect of corruption suggestions in career stage - Qualtrics' Quality Filter Sample  -----------------

# Generate labels for axis
Sug_Corr <- c("No suggestion","Suggestion","Suggestion","No suggestion")
GestLink <- c("Early stage", "Late stage", "Early stage","Late stage")

# Set coefficients of interest for bars
Y <- c(reg_1_h$coefficients[1], 
       (reg_1_h$coefficients[1] + reg_1_h$coefficients[2] + reg_1_h$coefficients[3] +reg_1_h$coefficients[4]), 
       (reg_1_h$coefficients[1] + reg_1_h$coefficients[2]), 
       (reg_1_h$coefficients[1] + reg_1_h$coefficients[3]))


graph_2d <- data.frame(Y, Sug_Corr, GestLink) %>% dplyr::filter(Sug_Corr == "No suggestion")

# Set coefficients of interest for bars
y_mind <- c(-2, reg_1_h$coefficients[1] + reg_1_h$coefficients[3] - reg_1_h$se[1]*1.96)
y_maxd <- c(-2, reg_1_h$coefficients[1] + reg_1_h$coefficients[3] + reg_1_h$se[1]*1.96)

# Plot by grids
d <- ggplot(graph_2d, aes(x = reorder(factor(GestLink), -Y), y = Y, fill = factor(GestLink))) +
   scale_fill_brewer(palette = "Paired") +
   geom_bar(stat = "identity", position = "dodge") + 
   geom_errorbar(aes(ymin = y_mind, ymax = y_maxd), width=.2,
                 position=position_dodge(.9)) +
   geom_text(aes(y = Y, label = round(Y, digits = 3), family = "serif"), colour = "black", size = 6,
             vjust = 7, position = position_dodge(.9))+
   labs(x = "\nCareer stage", 
        y = "Take-up rate agency services\n",
        title = 'No corruption suggestion') +
   theme_minimal() +
   theme(legend.position = "none", 
         text=element_text(size=16,  family="serif"),
         plot.title = element_text(hjust = 0.5)) + 
   ylim(0,0.8)



graph_2e <- data.frame(Y, Sug_Corr, GestLink) %>% dplyr::filter(Sug_Corr == "Suggestion")

# Set coefficients of interest for bars
y_mine <- c(reg_1_h$coefficients[1] + reg_1_h$coefficients[2] + reg_1_h$coefficients[3] +reg_1_h$coefficients[4] - reg_1_h$se[1]*1.96, -2)
y_maxe <- c(reg_1_h$coefficients[1] + reg_1_h$coefficients[2] + reg_1_h$coefficients[3] +reg_1_h$coefficients[4] + reg_1_h$se[1]*1.96, -2)


e <- ggplot(graph_2e, aes(x = reorder(factor(GestLink), Y), y = Y, fill = factor(GestLink))) +
   scale_fill_brewer(palette = "Paired") +
   geom_bar(stat = "identity", position = "dodge") + 
   geom_errorbar(aes(ymin = y_mine, ymax = y_maxe), width=.2,
                 position=position_dodge(.9)) +
   geom_text(aes(y = Y, label = round(Y, digits = 3), family = "serif"), colour = "black", size = 6,
             vjust = 9, position = position_dodge(.9))+
   labs(x = "\nCareer stage", 
        y = " ",
        title = 'Corruption suggestion') +
   theme_minimal() +
   theme(legend.position = "none", 
         text=element_text(size=16,  family="serif"),
         plot.title = element_text(hjust = 0.5)) + 
   ylim(0,0.8)

Sug_Corrb <- c("Suggestion","Suggestion")
GestLinkb <- c("Early stage","Late stage")
Y <- c( reg_1_h$coefficients[2], 
        (reg_1_h$coefficients[4] + reg_1_h$coefficients[2]))


graph_2f <- data.frame(Y, Sug_Corrb, GestLinkb) 

# Set coefficients of interest for bars
y_minf <- c(-2, reg_1_h$coefficients[4] + reg_1_h$coefficients[2] - reg_1_h$se[3]*1.96)
y_maxf <- c(-2, reg_1_h$coefficients[4] + reg_1_h$coefficients[2] + reg_1_h$se[3]*1.96)

f <- ggplot(graph_2f, aes(x = reorder(factor(GestLinkb), Y), y = Y, fill = factor(GestLinkb))) +
   scale_fill_brewer(palette = "Paired") +
   geom_bar(stat = "identity", position = "dodge") + 
   geom_errorbar(aes(ymin = y_minf, ymax = y_maxf), width=.2,
                 position=position_dodge(.9)) +
   geom_text(aes(y = Y, label = round(Y, digits = 3), family = "serif"), colour = "black", size = 6,
             vjust = -2, position = position_dodge(.9)) +
   labs(x = "\nCareer stage", 
        y = " ",
        title = 'Effect of suggestion') +
   theme_minimal() +
   theme(legend.position = "none", 
         text=element_text(size=16,  family="serif"),
         plot.title = element_text(hjust = 0.5))+ 
   ylim(-0.4, 0.4)

# Plot using patchwork (grids)
fig_a3 <- d + e + f

ggsave('Data/Output/Figures/fig_A3_transparent_corruption.pdf',width=20, height=10)


# ## ----------------------------------------------------------------------

rm(list = setdiff(ls(), c('tabla_survey', 'tab_1', 'tab_2', 'tab_3', 'tab_4', 
                          'tab_1_q', 'tab_2_q', 'tab_3_q', 'tab_h', 'tab_sq', 
                          'reg_1_c', 'reg_5_c', 'reg_9_c', 'reg_1_s', 'reg_1_h')))
